<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>backend</title>
    <link rel="stylesheet" type="text/css" href="/bootstarp/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="/css/index.css" />
    <link rel="stylesheet" type="text/css" href="/css/nmms.css" />
    <script src="/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="/bootstarp/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/userSetting.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/bootstrap-paginator.js" type="text/javascript" charset="utf-8"></script>
    <script>
        $(function () {

            // 分页
            let options = {
                bootstrapMajorVersion: 3,
                currentPage: "[[${pageInfo.pageNum == 0 ? 1 : pageInfo.pageNum}]]",
                totalPages: "[[${pageInfo.pages == 0 ? 1 : pageInfo.pages}]]",
                size: "normal",
                alignment: "center",
                pageUrl: function (type, page, current) {
                    return "/dept/show?pageNo=" + page;
                }
            }
            $("#page").bootstrapPaginator(options);

            $(".addDept").click(function () {
                $.ajax({
                    type: 'post',
                    url: '/dept/addDept',
                    data: {
                        name: $("#dept-name").val(),
                        duty: $("#dept-duty").val()
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            location.href = '/dept/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'> "+ res.message + " </div>",
                                tigger: "manual"
                            }).tooltip("show");
                        }

                    }
                })

            });

            $(".doAddSonDept").click(function () {
                $.ajax({
                    type: 'post',
                    url: '/dept/findById',
                    data:{
                        id: $(this).attr("data-id")
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            $("#parent_id").val(res.returnObject.deptId);
                            $("#parent_dept_name").val(res.returnObject.deptName);
                        } else {
                            $("#error").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'> "+ res.message + " </div>",
                                tigger: "manual"
                            }).tooltip("show");
                        }
                    }
                })
            });

            $(".addSonDept").click(function () {
                $(this).empty();
                $.ajax({
                    type: 'post',
                    url: '/dept/addSonDept',
                    data: {
                        fatherId: $("#parent_id").val(),
                        name: $("#dept_name").val(),
                        duty: $("#dept_duty").val()
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            location.href = '/dept/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'> "+ res.message + " </div>",
                                tigger: "manual"
                            }).tooltip("show");
                        }
                    }
                })
            });

            $(".doDisable").click(function () {
                $.ajax({
                    type: 'post',
                    url: '/dept/doDisable',
                    data: {
                        id: $(this).attr("data-id")
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            location.href = '/dept/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'> "+ res.message + " </div>",
                                tigger: "manual"
                            }).tooltip("show");
                        }
                        
                    }
                })

            });

            $(".doModifyDept").click(function () {
                $.ajax({
                    type: 'post',
                    url: '/dept/findById',
                    data: {
                        id: $(this).attr("data-id")
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            $("#deptId").val(res.returnObject.deptId);
                            $("#deptName").val(res.returnObject.deptName);
                            $("#deptDuty").val(res.returnObject.remark);
                        } else {
                            $("#error").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'> "+ res.message + " </div>",
                                tigger: "manual"
                            }).tooltip("show");
                        }

                    }
                })
            });

            $(".modifyDept").click(function () {
                $.ajax({
                    type: 'post',
                    url: '/dept/modifyDept',
                    data: {
                        id: $("#deptId").val(),
                        name: $("#deptName").val(),
                        duty: $("#deptDuty").val()
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            location.href = '/dept/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").tooltip({
                                title: "error",
                                placement: "center",
                                template: "<div class='tooltip errorMsg'> "+ res.message + " </div>",
                                tigger: "manual"
                            }).tooltip("show");
                        }

                    }
                })
            })




        });
    </script>
</head>

<body>

    <!-- 部门管理 -->
    <div class="panel panel-default" id="departmentSet">
        <div class="panel-heading">
            <h3 class="panel-title">部门管理<span id="error"></span></h3>
        </div>
        <div class="panel-body">
            <input type="button" value="添加部门" class="btn btn-primary" id="doAddDept">
            <br>
            <br>
            <div class="modal fade" tabindex="-1" id="Dept">
                <!-- 窗口声明 -->
                <div class="modal-dialog modal-lg">
                    <!-- 内容声明 -->
                    <div class="modal-content">
                        <!-- 头部、主体、脚注 -->
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">添加部门</h4>
                        </div>
                        <div class="modal-body text-center">
                            <div class="row text-right">
                                <label for="dept-name" class="col-sm-4 control-label">部门名称：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="dept-name">
                                </div>
                            </div>
                            <br>
                            <div class="row text-right">
                                <label for="dept-duty" class="col-sm-4 control-label">部门职能：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="dept-duty">
                                </div>
                            </div>
                            <br>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-primary addDept">添加</button>
                            <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 添加子部门模态框 -->
             <div class="modal fade" tabindex="-1" id="sonDept">
                <!-- 窗口声明 -->
                <div class="modal-dialog modal-lg">
                    <!-- 内容声明 -->
                    <div class="modal-content">
                        <!-- 头部、主体、脚注 -->
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">添加子部门</h4>
                        </div>
                        <div class="modal-body text-center">
                            <input type="hidden" id="parent_id"/>
                            <div class="row text-right">
                                <label for="dept-name" class="col-sm-4 control-label">父部门名称：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="parent_dept_name" readonly="readonly"/>
                                </div>
                            </div>
                            <br>
                            <div class="row text-right">
                                <label for="dept-name" class="col-sm-4 control-label">部门名称：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="dept_name">
                                </div>
                            </div>
                            <br>
                            <div class="row text-right">
                                <label for="dept-duty" class="col-sm-4 control-label">部门职能：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="dept_duty">
                                </div>
                            </div>
                            <br>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-primary addSonDept">添加</button>
                            <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                        </div>
                    </div>
                </div>
            </div>
            
            
            <!-- 修改部门模态框 -->
             <div class="modal fade" tabindex="-1" id="modifyDept">
                <!-- 窗口声明 -->
                <div class="modal-dialog modal-lg">
                    <!-- 内容声明 -->
                    <div class="modal-content">
                        <!-- 头部、主体、脚注 -->
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">修改部门</h4>
                        </div>
                        <div class="modal-body text-center">
                            <div class="row text-right">
                                <label for="dept-name" class="col-sm-4 control-label">id：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="deptId" readonly="readonly">
                                </div>
                            </div>
                            <br>
                            <div class="row text-right">
                                <label for="dept-name" class="col-sm-4 control-label">部门名称：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="deptName">
                                </div>
                            </div>
                            <br>
                            <div class="row text-right">
                                <label for="dept-duty" class="col-sm-4 control-label">部门职能：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="deptDuty">
                                </div>
                            </div>
                            <br>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-primary modifyDept">修改</button>
                            <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="show-list">
                <table class="table table-bordered table-hover" style='text-align: center;'>
                    <thead>
                        <tr class="text-danger">
                            <th class="text-center">序号</th>
                            <th class="text-center">部门编号</th>
                            <th class="text-center">部门名称</th>
                            <th class="text-center">部门职能</th>
                            <th class="text-center">所属部门</th>
                            <th class="text-center">部门状态</th>
                            <th class="text-center">操作</th>
                        </tr>
                    </thead>
                    <tbody id="tb">
                        <tr th:each="dept:${pageInfo.list}">
                            <td>[[${dept.deptId}]]</td>
                            <td>[[${dept.deptNo}]]</td>
                            <td>[[${dept.deptName}]]</td>
                            <td>[[${dept.remark}]]</td>
                            <td>
                                <span th:if="${dept.fatherDeptId == null}">--</span>
                                <span th:if="${dept.fatherDeptId != null}">[[${dept.fatherName}]]</span>
                            </td>
                            <td>
                                <span th:if="${dept.isValid == 1}">有效</span>
                                <span th:if="${dept.isValid == -1}">无效</span>
                            </td>
                            <td class="text-center">
                                <input type="button" class="btn btn-info btn-sm doAddSonDept" th:data-id="${dept.deptId}" value="添加子部门">
                                <input type="button" class="btn btn-warning btn-sm doModifyDept" th:data-id="${dept.deptId}" value="修改">
                                <span th:if="${dept.isValid == 1}">
                                    <input type="button" class="btn btn-success btn-sm doDisable" th:data-id="${dept.deptId}" value="启用">
                                </span>
                                <span th:if="${dept.isValid == -1}">
                                    <input type="button" class="btn btn-danger btn-sm doDisable" th:data-id="${dept.deptId}" value="禁用">
                                </span>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div style="text-align: center">
                    <ul id="page"></ul>
                </div>
            </div>
        </div>
    </div>
</body>

</html>